package com.example.coursaccino;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class WelcomeActivity extends Activity {
	
	public final static String USERNAME = "com.example.myfirstapp.USERNAME";
	public final static String PASSWORD = "com.example.myfirstapp.PASSWORD";

	public static ModelDbHelper dbHelper = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        System.out.println("Reached in on create");
        // this.deleteDatabase("Coursaccino.db");
        /* create the database tables if not there */
        dbHelper = new ModelDbHelper(this);
        setContentView(R.layout.activity_welcome);
    }
    
    @Override
    public void onResume() {
    	super.onResume(); 
    	System.out.println("Reached in on resume");
    }

    /* Called when the user clicks the Login button button */
    public void checkLogin (View view) {
    	/* instantiate the login intent */
    	Intent intent = new Intent(this, RegisterActivity.class);
    	
    	/* get username and create a username key-value pair */
    	EditText username_et = (EditText) findViewById(R.id.username_et);
    	String username = username_et.getText().toString();
    	
    	
    	/* get password and create a password key-value pair */
    	EditText password_et = (EditText) findViewById(R.id.password_et);
    	String password = password_et.getText().toString();
    	
    	// TextView errorMsg;
    	
    	System.out.println("Login request" + username + " " + password);
    	
    	if (WelcomeActivity.dbHelper.getModel().getUserDAO().
    			validateUser(username, password)) {
    		
			intent.putExtra(USERNAME, username);
	    	intent.putExtra(PASSWORD, password);
	    	System.out.println("Credentials correct");
	    	startActivity(intent);
    	}
    	else {
//    		
//    		TextView errorMsg = (TextView) findViewById(R.id.error_welcome);
//    		errorMsg.setText("Credentials Incorrect");
//    		errorMsg.setVisibility(0);
        	Toast msg = Toast.makeText(getBaseContext(),"Credentials Incorrect !", Toast.LENGTH_LONG);
        	msg.show();
    	}
    }
    
    public void register (View view) {
    	Intent intent = new Intent(this, RegisterActivity.class);    	
    	startActivity(intent);
    }
}
